package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.cache.ConfigCache;
import com.framework.loippi.entity.TLogistics;
import com.framework.loippi.entity.TOrder;
import com.framework.loippi.express.ExpressService;
import com.framework.loippi.service.TLogisticsService;
import com.framework.loippi.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("apiLogisticsController")
@RequestMapping("/api/logistics")
@Api(tags = {"订单物流信息"})
public class LogisticsController {

    @Resource
    private TLogisticsService logisticsService;
    @Resource
    private TOrderService orderService;

    @RequestMapping(value = "/findByOrderId.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiParam(name = "orderId", value="订单id", required = true)
    @ApiOperation(value = "查询订单物流信息", notes = "{state: 1已发送,2已送达,3已签收,4已取消}")
    public @ResponseBody
    String findAllByUId(Long orderId) {
        String key = ConfigCache.getConfig("express.100.key");
        String customer = ConfigCache.getConfig("express.100.customer");
        String secret = ConfigCache.getConfig("express.100.secret");
        String userId = ConfigCache.getConfig("express.100.userid");

        TOrder order = orderService.findDetailById(orderId);
        TLogistics tLogistics = new TLogistics();
        List<TLogistics> list = new ArrayList<>();

        if (null != order.getLogisticsCompany() && null != order.getLogisticsNumber()) {
            // 快递公司编号
            String expressCompany = order.getLogisticsCompany();
            // 订单编号
            String orderNum = order.getLogisticsNumber();
            // 手机号码后四位
            String phone = order.getPhone().substring(order.getPhone().length() - 4);
            String from = "";                //出发地
            String to = "";                    //目的地
            int resultv2 = 0;                //开启行政规划解析
            ExpressService expressService = new ExpressService(key, customer);
            String result = expressService.synQueryData(expressCompany, orderNum, phone, from, to, resultv2);
            JSONObject json = JSONObject.fromObject(result);
            tLogistics.setLogisticsNum(json.getString("nu"));
            switch (json.getString("com")) {
                case "zhongtong":
                    tLogistics.setLogisticsName("中通快递");
                    break;
                case "shunfeng":
                    tLogistics.setLogisticsName("顺风速运");
                    break;
                case "yunda":
                    tLogistics.setLogisticsName("韵达快递");
                    break;
                case "shentong":
                    tLogistics.setLogisticsName("申通快递");
                    break;
                case "yuantong":
                    tLogistics.setLogisticsName("圆通速递");
                    break;
                case "huitongkuaidi":
                    tLogistics.setLogisticsName("百世快递");
                    break;
                case "debangkuaidi":
                    tLogistics.setLogisticsName("德邦快递");
                    break;
                case "youzhengguonei":
                    tLogistics.setLogisticsName("邮政快递包裹");
                    break;
                case "tiantian":
                    tLogistics.setLogisticsName("天天快递");
                    break;

            }
            if (json.getString("state").equals("0")) {
                tLogistics.setLogisticsState(1);
            }
            if (json.getString("state").equals("1")) {
                tLogistics.setLogisticsState(2);
            }
            if (json.getString("state").equals("3")) {
                tLogistics.setLogisticsState(5);
            }

            JSONArray jsonArray = json.getJSONArray("data");
            for (Object o : jsonArray) {
               tLogistics.setContent((JSONObject.fromObject(o).getString("context")));
                try {
                    tLogistics.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONObject.fromObject(o).getString("ftime")));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                list.add(tLogistics);
            }
            
        }
        Map<String, Object> map = new HashMap<>();
        map.put("top", list.get(0));
        map.put("list", list);
        return ApiUtils.success(map);
    }
}
